package com.develop.archives.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.develop.archives.domain.CommunityDO;
import com.develop.archives.service.CommunityService;
import com.develop.common.utils.PageUtils;
import com.develop.common.utils.Query;
import com.develop.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-04-08 14:45:26
 */
 
@Controller
@RequestMapping("archives/community")
public class CommunityController {
	@Autowired
	private CommunityService communityService;
	
	@GetMapping()
	@RequiresPermissions("archives:community:community")
	String Community(){
	    return "archives/community/community";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("archives:community:community")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CommunityDO> communityList = communityService.list(query);
		int total = communityService.count(query);
		PageUtils pageUtils = new PageUtils(communityList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("archives:community:add")
	String add(){
	    return "archives/community/add";
	}

	@GetMapping("/edit/{communityid}")
	@RequiresPermissions("archives:community:edit")
	String edit(@PathVariable("communityid") String communityid,Model model){
		CommunityDO community = communityService.get(communityid);
		model.addAttribute("community", community);
	    return "archives/community/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("archives:community:add")
	public R save( CommunityDO community){
		if(communityService.save(community)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("archives:community:edit")
	public R update( CommunityDO community){
		communityService.update(community);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("archives:community:remove")
	public R remove( String communityid){
		if(communityService.remove(communityid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("archives:community:batchRemove")
	public R remove(@RequestParam("ids[]") String[] communityids){
		communityService.batchRemove(communityids);
		return R.ok();
	}
	
}
